package assignment2;

import java.util.Scanner;

/**
 * A simple textual interface example exercising the Ellipse class.
 */
public class MainEllipse {
  
  public static void main (String[] argv) {
    (new EllipseTUI()).start();
  }
}

/**
 * A simple text-based user interface for creating and exercising an Ellipse.
 */
class EllipseTUI {
 
  // User menu choices:
  private static final int NO_CHOICE = 0;
  private static final int MAJ_AXS = 1;
  private static final int MIN_AXS = 2;
  private static final int AREA = 3;
  private static final int CIRCUMF = 4;
  private static final int NEW = 5;
  private static final int EXIT = 6;
  
  private EllipseArray ellipses;
  private Scanner in;
  private java.io.PrintStream out = System.out;

  /**
   * Create a user interface.
   */
  public EllipseTUI () {
    this.ellipses = null;
    this.in = new Scanner(System.in);
  }

  /**
   * Run the user interface.
   */
  public void start () {
    createEllipse();
    int choice = NO_CHOICE;
    while (choice != EXIT) {
      displayMenu();
      choice = readIntWithPrompt("Enter choice: ");
      executeChoice(choice);
    }
  }

  /**
   * Create an Ellipse with dimensions provided by the user.
   */
  private void createEllipse () {
    int major = -1;
    int minor = -1;
    while (major < 0)
      major = readIntWithPrompt("Ellipse maj axis (a non-negative integer): ");
    while (minor < 0)
      minor = readIntWithPrompt("Ellipse min axis (a non-negative integer): ");
    this.ellipses = new EllipseArray( new Ellipse(major,minor) );
  }
 
  /**
   * Display top level menu.
   */
  private void displayMenu () {
    out.println("\nEnter the number denoting the action to perform:" +
    "\nDisplay major axis.........." + MAJ_AXS +
    "\nDisplay minor axis.........." + MIN_AXS +
    "\nDisplay area................" + AREA    +
    "\nDisplay circumference......." + CIRCUMF +
    "\nCreate new Ellipse.........." + NEW     +
    "\nExit........................" + EXIT    + "\n" );
  }

  /**
   * Execute user's menu choice.
   */
  private void executeChoice (int choice) {
    out.println();
    if (choice == MAJ_AXS)
      out.println("Major axis is    " + ellipses.get().majorAxis());
    else if (choice == MIN_AXS)
      out.println("Minor axis is    " + ellipses.get().minor_axis());
    else if (choice == AREA)
      out.println("Area is          " + ellipses.get().area());
    else if (choice == CIRCUMF)
      out.println("Circumference is " + ellipses.get().circumference());
    else if (choice == NEW)
      createEllipse();
    else if (choice == EXIT)
      out.println("Goodbye.");
  }
  
  /**
   * Read an int from system in with the speciifed prompt.
   */
  private int readIntWithPrompt (String prompt) {
    out.print(prompt); out.flush();
    while (!in.hasNextInt()) {
      in.nextLine();
      out.print(prompt); out.flush();
    }
    int input = in.nextInt();
    in.nextLine();
    return input;
  }  
}